package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/17 20:28
 */
@SuppressWarnings("all")
@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    DtsBrandMapper dtsBrandMapper;

    @Override
    public List<DtsBrand> findByAdminId(Integer id) {

        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId,id);

        List<DtsBrand> brands = dtsBrandMapper.selectList(wrapper);
        return brands;
    }

    /**
     * 查询所有的品牌
     */
    @Override
    public List<Map<String, Object>> findList() {

        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> brands = dtsBrandMapper.selectList(wrapper);

        List<Map<String, Object>> collect = brands.stream().map(dtsBrand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsBrand.getId());
            map.put("label", dtsBrand.getName());
            return map;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public DtsBrand findById(Integer brandId) {
        DtsBrand brand = dtsBrandMapper.selectById(brandId);
        return brand;
    }

    /**
     * 品牌制造商列表
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String order, String sort, Integer id, String name) {

        IPage<DtsBrand> iPage = new Page<>(page,limit);

        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();

        if(id!=null){
            wrapper.eq("id", id);
        }
        if(name!=null){
            wrapper.like("name",name);
        }
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("deleted",false);

        dtsBrandMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());

        List<DtsBrand> records = iPage.getRecords();

        List<DtsBrand> items = new ArrayList<>();
        for (DtsBrand record : records) {
            items.add(record);
        }
        map.put("items",items);
        return map;
    }

    @Override
    public void insert(DtsBrand dtsBrand) {
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrand.setDeleted(false);
        dtsBrandMapper.insert(dtsBrand);
    }

    @Override
    public void update(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public void delete(DtsBrand dtsBrand) {
        dtsBrand.setDeleted(true);
        dtsBrandMapper.updateById(dtsBrand);
    }
}
